/**
 * Created with IntelliJ IDEA.
 * User: ajhin
 * Date: 3/22/14
 * Time: 2:08 PM
 * To change this template use File | Settings | File Templates.
 */
package com.societyGames.codeAlong.state.codeExercise.model.testCase
{
import flash.events.Event;

public class WaitForCanExecuteState extends TestCaseControllerState implements IStoppable
{
  public function WaitForCanExecuteState(testCaseController:TestCaseController)
  {
    super(testCaseController);
  }

  override public function enter():void
  {
    if (_testCaseController.selectedTestCase.canExecute)
    {
      _testCaseController._stateMachine.gotoState(_testCaseController._executeState);
    }
    else
    {
      _testCaseController.selectedTestCase.testCaseStatusChangedEventObject.addEventListener(handleTestCaseStatusChanged);
    }
  }

  public function handleTestCaseStatusChanged(event:Event):void
  {
    if (_testCaseController.selectedTestCase.canExecute) //We're in the state we want.
    {
      _testCaseController.selectedTestCase.testCaseStatusChangedEventObject.removeEventListener(handleTestCaseStatusChanged);
      _testCaseController._stateMachine.gotoState(_testCaseController._executeState);
    }
  }

  public function stop():void
  {
    _testCaseController.selectedTestCase.testCaseStatusChangedEventObject.removeEventListener(handleTestCaseStatusChanged);
    _testCaseController._stateMachine.gotoState(_testCaseController._failState); //TODO: Should this be StoppedState?
  }
}
}
